package com.example.carrierapp.ui.main.goodssource.history

import android.content.Context
import com.example.carrierapp.base.BasePresenter
import com.example.carrierapp.bean.RowsBean
import com.example.carrierapp.config.toast
import com.example.carrierapp.net.ApiMiddleHelper
import com.example.carrierapp.net.listener.DataCallBackListener
import com.example.carrierapp.util.LogUtil
import com.example.carrierapp.viewmodel.itemmodel.ItemGoodsSourceModel
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken

/**
 * 货源大厅
 */
class CompetitionHistoryPresenter(private val context: Context) : BasePresenter<CompetitionHistoryContract.View>(), CompetitionHistoryContract.Presenter {

    override fun fetchGoodsSourceList(page: Int) {
        val params = HashMap<String, String>()
        params["maxResultCount"] = "10"
        params["skipCount"] = page.toString()
        params["pageIndex"] = page.toString()
        ApiMiddleHelper.getInstance().toNetIo2Main(context, apiStrores.fetchGoodsRecordList(params), object : DataCallBackListener {
            override fun onSuccess(json: String?) {
                json?.let {
                    LogUtil.response("FetchGoodsSourceListSuccess : ${GsonBuilder().setPrettyPrinting().create().toJson(Gson().fromJson<RowsBean<ItemGoodsSourceModel>>(it, object : TypeToken<RowsBean<ItemGoodsSourceModel>>() {}.type))}")
                    view.onFetchSuccess(Gson().fromJson<RowsBean<ItemGoodsSourceModel>>(it, object : TypeToken<RowsBean<ItemGoodsSourceModel>>() {}.type))
                }
            }

            override fun onFailure(msg: String?) {
                toast(msg)
                LogUtil.v("FetchGoodsSourceListError : $msg")
                view.onFetchError()
            }
        })
    }

    override fun start() {

    }


}
